//
//  LSZMoveCheckVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2017/9/12.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LSZMoveCheckVC.h"
 
@interface LSZMoveCheckVC ()

@end

@implementation LSZMoveCheckVC
 
#pragma mark - 查看有没有缓存
-(void)ViewTheDataCacheOnTheAudioAlbumPage :(NSInteger)page
{
    __weak typeof(self) weakSelf = self;
    if ([self.chooseTitleValue isEqualToString:@"歌手/牧师"]) {
        self.chooseTitleValue = @"歌手";
    }
    NSString * str  = [NSString stringWithFormat:@"%@_%@_%@",shouye_yinpin,self.chooseTitleValue,self.chooseType];
    [LSZCacheHandling initWithfindPlist:str :^(BOOL isOk, NSDictionary * dic) {
        if (isOk == YES) {
            LSZAudioManager * MG  = [[LSZAudioManager alloc]initWithAudioManager:dic];
            [weakSelf.mutarrayData removeAllObjects];                    [weakSelf.mutarrayData addObjectsFromArray:MG.mutDataArray];
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [weakSelf.collectionViewMove reloadData];
            }];
        }else{
            [self initWithNumberPage:page];
        }
    }];
}
#pragma mark - 请求的数据
-(void)initWithNumberPage :(NSInteger)page
{
    __weak typeof(self) weakSelf = self;
    [LSZRequestResolutionDic audioMoreClassification:[NSString stringWithFormat:@"%ld",(long)page] type:self.chooseType chooseTitleValue:self.chooseTitleValue :^(BOOL isOK, NSString * Str, LSZAudioManager *audioManager) {
        if (isOK == YES) {
            if (page == 1) {
                [weakSelf.mutarrayData removeAllObjects];
                [weakSelf.mutarrayData addObjectsFromArray:audioManager.mutDataArray];
            }else{
                [weakSelf.mutarrayData addObjectsFromArray:audioManager.mutDataArray];
            }
            if(audioManager.mutDataArray.count == 0){
                [weakSelf.collectionViewMove.mj_footer endRefreshingWithNoMoreData];
            }
            [weakSelf.collectionViewMove reloadData];
        }else{
            [self setStringWarning:Str];
        }
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.chooseTitleValue;
    self.mutarrayData  = [[NSMutableArray alloc]init];
    self.numberPage = 1;

    [self ViewTheDataCacheOnTheAudioAlbumPage:self.numberPage];
    [self setUI];
    //诗歌歌谱的上下拉
    self.collectionViewMove.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.numberPage = 1;
        //延时
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self ViewTheDataCacheOnTheAudioAlbumPage :self.numberPage];
            [self.collectionViewMove.mj_header endRefreshing];
        });
    }];
    // 上拉刷新
    self.collectionViewMove.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.numberPage++;
        //延时
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self initWithNumberPage:self.numberPage];
            [self.collectionViewMove.mj_footer endRefreshing];
        });
    }];
    self.view.backgroundColor = RGBColor(236, 236, 236);
}
-(void)setUI
{
    CGFloat width = (ScreenWidth - 40-1) / 3;

    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(width,width+ 42);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(10 , 5,10,5);
    self.collectionViewMove = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight) collectionViewLayout:flowLayout];
    self.collectionViewMove.delegate   = self;
    self.collectionViewMove.dataSource = self;
    self.collectionViewMove.backgroundColor = RGBColor(236, 236, 236);
    [self.view addSubview:self.collectionViewMove];

    [self.collectionViewMove registerClass:[LSZMoveCollectionViewCell class] forCellWithReuseIdentifier:@"LSZMoveCollectionViewCell"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.mutarrayData.count;
}
#pragma mark - 单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LSZMoveCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LSZMoveCollectionViewCell" forIndexPath:indexPath];
    LSZAudioMg * audioMg = nil;
    audioMg = self.mutarrayData[indexPath.row];
    [cell.imagePicture  sd_setImageWithURL:[NSURL URLWithString:audioMg.image] placeholderImage:[UIImage imageNamed:@"我的_设置_关于"]];
    if ([audioMg.type isEqualToString:@"zhuanji"]) {
        cell.labelName.text = [NSString stringWithFormat:@"%@",audioMg.album_name];
        NSString * str = [NSString stringWithFormat:@"%@",audioMg.artist_name];
        cell.labelIntroduce.text =NullStringjudgment(str);
    }else{
        cell.labelName.text = [NSString stringWithFormat:@"%@",audioMg.singerName];
        [cell.labelName mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(cell.bgview.mas_centerY);
            make.width.equalTo(cell.bgview.mas_width);
            make.height.offset(8);
        }];
    }
    cell.labelTotal.text = [NSString stringWithFormat:@"共%@首",audioMg.total_song];
    cell.labelClick.text = audioMg.hits;
    CGFloat width = [LSZSpecialPackaging getsTheAdaptiveWidth:cell.labelClick sizeMaxWidth:cell.imagePicture.frame.size.width]+3;
    [cell.labelClick mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(width);
    }];
    cell.backgroundColor = RGBColor(236, 236, 236);
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LSZNewAudioPlayImageAndTableViewVC * vc = [[LSZNewAudioPlayImageAndTableViewVC alloc]init];
    LSZAudioMg * audioMg = nil;
    audioMg = self.mutarrayData[indexPath.row];
    [LCUtils moveAlbumArtistDetailsInterfaceJump:vc audioMg:audioMg typeComeWhere:@"1"];
    vc.lastPageNumberRow = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    vc.lastSection = [NSString stringWithFormat:@"%ld",(long)indexPath.section-1];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
